package com.bonc.situation.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 * HttpClient
 * 调用服务接口
 * @author 1
 *
 */
public class HttpClientUtils {
	private static int CONNECTIONOUTTIME=10*1000;//10秒
	static Properties props =null;
	static String AUTH="";
	public HttpClientUtils(Properties props) {
		super();
		try {
			props = PropertiesLoaderUtils.loadAllProperties("systemParam.properties");
			AUTH=props.getProperty("x5_bpm.auth");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	static{
		try {
			props = PropertiesLoaderUtils.loadAllProperties("systemParam.properties");
			AUTH=props.getProperty("x5_bpm.auth");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查找代办事宜
	 * @throws HttpException
	 * @throws IOException
	 */
	public static List<Object> todoList(String auth,Map<String,Object> param) throws IOException{
		List<Object> list = new ArrayList<Object>();
		String url = props.getProperty("restful.task.getTodoList");
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTIONOUTTIME);
		PostMethod post = new PostMethod(url);
		Gson gson = new Gson();
		String requestStr=gson.toJson(param);
		post.setRequestHeader("ContentType", "application/json;charset=utf-8");
//		post.setRequestHeader("Authorization", "Basic "+com.ufc.util.Base64Utils.encodeData(AUTH));
		RequestEntity entity = new StringRequestEntity(requestStr,"application/json","UTF-8");
		post.setRequestEntity(entity);
		int code=client.executeMethod(post);
		if(code==200){
			String json=post.getResponseBodyAsString();
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(json).getAsJsonObject(); 
			JsonArray jsonArray = jsonObject.getAsJsonArray("rows");
			for (int i = 0; i < jsonArray.size(); i++) {  
				//获取第i个数组元素  
				JsonElement el = jsonArray.get(i);  
				//映射为类实例  
				Object data = gson.fromJson(el, Object.class);  
				if(data!=null){
					list.add(data);
				}
			} 
		}
		return list;
	}
	/**
	 * 根据任务ID获取任务 10000000050510
	 */
	public void getTaskByTaskId() throws HttpException, IOException{
		String url="http://url:8085/x5mini/restful/task/getTaskByTaskId?taskId=10000000050915";
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTIONOUTTIME);
		GetMethod  get = new GetMethod(url);
		get.setRequestHeader("ContentType", "application/json;charset=utf-8");
//		get.setRequestHeader("Authorization", "Basic "+com.ufc.util.Base64Utils.encodeData("guw:1"));
		int code=client.executeMethod(get);
		if(code==200){
			System.out.println(get.getResponseBodyAsString()); 
		}
		String url2="http://url:8085/x5mini/restful/task/setTaskVarLocal?taskId=10000000050876";
		PostMethod post = new PostMethod(url2);
		post.setRequestHeader("ContentType", "application/json;charset=utf-8");
//		post.setRequestHeader("Authorization", "Basic "+com.ufc.util.Base64Utils.encodeData("guw:1"));
		String requestStr="{\"bxje\":\"5000\"}";
		RequestEntity entity = new StringRequestEntity(requestStr,"application/json","UTF-8");
		post.setRequestEntity(entity);
		int code2=client.executeMethod(post);
		if(code2==200){
			System.out.println(post.getResponseBodyAsString()); 
		}
	}
}
